package com.richfit.cuba.modular.lsms.uploadFile.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.richfit.cuba.modular.lsms.uploadFile.entity.UkeyUploadfile;
import com.richfit.cuba.modular.lsms.uploadFile.service.IUkeyUploadfileService;
import com.richfit.cuba.modular.lsms.uploadFile.util.CertificateType;
import com.richfit.cuba.web.core.common.constant.factory.PageFactory;

import cn.eonshine.base.core.base.controller.BaseController;
import cn.eonshine.base.core.base.tips.ErrorTip;

/**
 * 控制器
 *
 * @author fengshuonan
 * @Date 2018-11-07 11:46:22
 */
@Controller
@RequestMapping("/tampering")
public class TamperingController extends BaseController {
	@Value("${local.tampering}")
	private String localPath;
	
	
    private String PREFIX = "/tampering/";

    @Autowired
    private IUkeyUploadfileService ukeyUploadfileService;

    /**
     * 跳转到首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "tampering.html";
    }

    /**
     * 跳转到添加
     */
    @RequestMapping("/tampering_add")
    public String ukeyUploadfileAdd() {
        return PREFIX + "tampering_add.html";
    }

    /**
     * 跳转到修改
     */
    @RequestMapping("/tampering_update/{ukeyUploadfileId}")
    public String ukeyUploadfileUpdate(@PathVariable String ukeyUploadfileId, Model model) {
        UkeyUploadfile ukeyUploadfile = ukeyUploadfileService.getById(ukeyUploadfileId);
        model.addAttribute("item",ukeyUploadfile);
//        LogObjectHolder.me().set(ukeyUploadfile);
        return PREFIX + "tampering_edit.html";
    }

    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
    	Page<UkeyUploadfile> page = new PageFactory<UkeyUploadfile>().defaultPage();
    	QueryWrapper<UkeyUploadfile> queryWrapper = new QueryWrapper<UkeyUploadfile>();
    	if(StringUtils.isNotBlank(condition)) {
    		queryWrapper.like("remarks", condition);
    	}
    	queryWrapper.eq("CERTIFICATE_TYPE", CertificateType.CERTIFICATE_TYPE_DATA_TAMPER);
    	//queryWrapper.orderByDesc("UPDATE_DATE");
    	page = (Page<UkeyUploadfile>) ukeyUploadfileService.page(page, queryWrapper);
        return packForBT(page);
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(UkeyUploadfile ukeyUploadfile) {
//    	Date date=DateUtils.getCubaDate(new Date());
//    	ukeyUploadfile.setCreateDate(date);
//    	ukeyUploadfile.setUpdateDate(date);
//    	ShiroUser user = ShiroKit.getUser();
//    	ukeyUploadfile.setCertificateType(CertificateType.CERTIFICATE_TYPE_DATA_TAMPER);
//    	ukeyUploadfile.setCreateBy(user.getId().toString());
//    	ukeyUploadfile.setUpdateBy(user.getId().toString());
//        ukeyUploadfileService.insert(ukeyUploadfile);
    	
    	try {
			ukeyUploadfileService.saveUpload(ukeyUploadfile, CertificateType.CERTIFICATE_TYPE_DATA_TAMPER);
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorTip(400, e.getMessage());
		}
    	
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam String ukeyUploadfileId) {
        ukeyUploadfileService.removeById(ukeyUploadfileId);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(UkeyUploadfile ukeyUploadfile) {
    	try {
			ukeyUploadfileService.saveUpload(ukeyUploadfile, CertificateType.CERTIFICATE_TYPE_DATA_TAMPER);
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorTip(400, e.getMessage());
		}
        return SUCCESS_TIP;
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{ukeyUploadfileId}")
    @ResponseBody
    public Object detail(@PathVariable("ukeyUploadfileId") String ukeyUploadfileId) {
        return ukeyUploadfileService.getById(ukeyUploadfileId);
    }
    
    @RequestMapping(value="/file") 
    @ResponseBody  
    public String uploadFile(HttpServletRequest request, HttpServletResponse response) throws IllegalStateException, IOException {  
    	String path =ukeyUploadfileService.uploadFile(request,response,localPath);
        return path;  
    }  
      
     
}
